
class MyString {
    String s = "";
}

class kyo {
    // String s = new String();
    String s = "hello";

    void show() {
        s += "world";
        System.out.println(s.length());
        System.out.println(s);
    }
}

public class StringTest {
    static public void test(MyString obj) {
        System.out.printf("test s = %s\n", obj.s);
        obj.s = "world";
    }

    static public void testMem() {
        // kyo o = new kyo();
        // o.show();

        String a = "hello";
        String b = a;

        System.out.printf("a = %s, b = %s\n", a, b);
        b = "world";
        System.out.printf("a = %s, b = %s\n", a, b);

        // test(a);
        // System.out.printf("a = %s, b = %s\n", a, b);

        MyString obj = new MyString();
        obj.s = "hello";

        test(obj);
        System.out.printf("obj.s = %s\n", obj.s);
    }

    static public void main(String [] args) {
        // 测试字符串内存
        // testMem();

        String s = "   hello wor ld   ";

        System.out.printf("s = %s, len = %d\n", s, s.length());
        System.out.printf("indexof w = %d\n", s.indexOf('w'));
        System.out.printf("indexof lo = %d\n", s.indexOf("lo"));
        System.out.printf("lastIndexof l = %d\n", s.lastIndexOf('l'));

        int len = s.length();
        for (int i = 0, c = 0; i < len; ++i, ++c) {
            if (c == 7)
                c = 0;
            System.out.printf("\033[%dm%c\033[0m ", 30 + c, s.charAt(i));
        }
        System.out.println();

        System.out.printf("substring 3 = %s\n", s.substring(3));
        System.out.printf("substring 3 7 = %s\n", s.substring(3, 7));

        System.out.printf("===> trim = #%s#\n", s.trim());

        System.out.printf("s = %s, len = %d\n", s, s.length());

        String a = new String("hello");
        String b = new String("hello");
        // String a = "hello";
        // String b = a;
        b = a;

        // String b = "hello";
        System.out.printf("a = %s, b = %s\n", a, b);


        System.out.println((a == b));

    }
}
